import QtQuick 1.1
import "../../../qml-ui/"
import "../../../qml-ui/js/app.js" as LIBAPP
import "js/fb-app.js" as APP

Rectangle {

    property string your_facebook_id: "266475193416007";

    id: main
    property string link: "http://www.sme.sk";
    property string fbid: facebook.user_facebook_id
    property string fbtoken: facebook.user_facebook_token
    property bool logged_in: facebook.user_is_authenticated

    property alias inputText: reviewTextEdit.text
    property alias inputFocus: reviewTextEdit.focus
    property string title: ""
    property string ebayUrl: ""


    Background {
        id: bg
    }

    states: [
        State {
            name: "logged"
            PropertyChanges { target: facebook; visible: false; }
            PropertyChanges { target: facebook; height: 0; }
            PropertyChanges { target: cancelButton; visible: false; }
            PropertyChanges { target: cancelButton; height: 0; }
            PropertyChanges { target: shareColumn; visible: true; }
            PropertyChanges { target: bg; visible: true; }
        }
    ]

    Facebook {
        id: facebook
        width: parent.width - 32 * APP.scale
        height: parent.height - 104 * APP.scale
        anchors.top: parent.top
        anchors.topMargin: 16 * APP.scale
        anchors.horizontalCenter: parent.horizontalCenter

        property string facebook_display_style: "touch" // or "wap"
        property string facebook_application_id: main.your_facebook_id;

        function whenUserIsAuthenticated(userid, usertoken){
            main.state = "logged"

        }
    }

    Button {
        id: cancelButton
        buttonImage: APP.getProjectImage("buttons/button_middleBlack_btn.png");
        buttonImagePressed: APP.getProjectImage("buttons/button_middleBlack_pressed_btn.png");
        buttonText: "CANCEL"
        buttonTextColor: "#FFFFFF"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 16 * APP.scale
        width: 206 * APP.scale
        height: 51 * APP.scale
        onClicked: {
            Qt.quit();
        }
    }

    MouseArea {
        anchors.fill: if (main.state == "logged") parent
        onClicked: { reviewTextEdit.focus = false; }
    }

    Column{
        id: shareColumn
        width: parent.width - 64 * APP.scale
        spacing: 16 * APP.scale
        anchors.centerIn: parent
        visible: false

        LibraryImage{
            id: lImage
            image: "icon/facebook_icon.png";
            visible: false
        }

        Image {
            id: image1
            source: lImage.source
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Text {
            font.pixelSize: 26 * APP.scale;
            width: parent.width
            wrapMode: Text.WordWrap;
            color: "#ffffff"
            text: "Share on Facebook"
            horizontalAlignment: Text.AlignHCenter
        }

        Text {
            text:  title;
            font.pixelSize: 22 * APP.scale;
            width: parent.width
            wrapMode: Text.WordWrap;
            color: "#3B5998"
            horizontalAlignment: Text.AlignHCenter
        }

        FocusScope {
            id: focusScope
            width: parent.width
            height: 181 * APP.scale
            z: 10

            LibraryImage {
                id: libImage
                visible: false
                image: "messageBox_bg.png"
            }
            BorderImage{
                id: inactive
                source: libImage.source
                width: parent.width; height: parent.height
                border { left: 16 * APP.scale; top: 16 * APP.scale; right: 16 * APP.scale; bottom: 16 * APP.scale }
            }

            Text {
                id: typeSomething
                width: parent.width - 16 * APP.scale
                height: parent.height - 16 * APP.scale
                anchors.centerIn: parent;
                text: "Write your message"
                color: "gray"
                font.pixelSize: 22 * APP.scale
            }

            MouseArea {
                anchors.fill: parent
                onClicked: { focusScope.focus = true; reviewTextEdit.openSoftwareInputPanel(); }
            }

            TextEdit{
                id: reviewTextEdit
                property int maximumLength: 200
                property string previousText: text
                width: parent.width - 16 * APP.scale
                height: parent.height - 16 * APP.scale
                anchors.centerIn: parent;
                font.pixelSize: 22 * APP.scale;
                color: "#fff";
                wrapMode: TextEdit.WordWrap
                cursorPosition: text.length
                onTextChanged: {
                    if (text.length > maximumLength) {
                        var cursor = cursorPosition;
                        text = previousText;
                        if (cursor > text.length) {
                            cursorPosition = text.length;
                        } else {
                            cursorPosition = cursor-1;
                        }
                    }
                    previousText = text
                }
            }

            states: State {
                name: "hasText"; when: reviewTextEdit.text != ''
                PropertyChanges { target: typeSomething; opacity: 0 }
            }

            transitions: [
                Transition {
                    from: ""; to: "hasText"
                    NumberAnimation { exclude: typeSomething; properties: "opacity" }
                },
                Transition {
                    from: "hasText"; to: ""
                    NumberAnimation { properties: "opacity" }
                }
            ]
        }

        Item{
            width: parent.width
            height: childrenRect.height


            Button {
                id: shareCancelButton
                buttonImage: APP.getProjectImage("buttons/button_middleBlack_btn.png");
                buttonImagePressed: APP.getProjectImage("buttons/button_middleBlack_pressed_btn.png");
                buttonText: "LOGOUT"
                buttonTextColor: "#FFFFFF"
                width: parent.width/2

                height: 51 * APP.scale
                onClicked: {
                    //                     Qt.quit();
                }
            }

            Button {
                id: shareButton
                buttonImage: APP.getProjectImage("buttons/button_middleBlue_btn.png");
                buttonImagePressed: APP.getProjectImage("buttons/button_middleBlue_pressed_btn.png");
                buttonTextColor: "#FFFFFF"
                buttonText: "SHARE"
                //                width: 206 * APP.scale
                width: parent.width/2
                height: 51 * APP.scale
                anchors.right: parent.right

                onClicked: {
                    var params = "&access_token=" + facebook.user_facebook_token;
                    params += "&link=" + link;
                    params += "&message=" + reviewTextEdit.text;
                    publishFeed(params);
                }


                function publishFeed(params){
                    var graphUrl = "https://graph.facebook.com/";

                    var  url1 = graphUrl + "/me/feed";
                    var headers = [{name:'Accept', value: 'application/json'}, {name:'Content-type', value: 'APPlication/json'}];

                    var xhr = new XMLHttpRequest;
                    xhr.open("POST", url1);
                    if (headers) {
                        for (var i = 0; i<headers.length; i++) {
                            xhr.setRequestHeader(headers[i].name, headers[i].value);
                        }
                    }

                    xhr.onreadystatechange = function() {//Call a function when the state changes.
                        if(xhr.readyState == 4 && xhr.status == 200) {
                            var text = "Item shared successfuly!";
                            showNotification(text);
                            timer.start();
                        }
                        else{
                            var text = "Feed action request limit reached! Try again later.";
                            showNotification(text);
                        }
                    }
                    xhr.send(params);
                }
            }
        }
    }

    Notification {
        id: notifications
    }


    function showNotification(text) {
        notifications.text = text;
        notifications.show();
        notifications.visible = true;
        timer.start();
    }

    Timer {
        id: timer
        interval: 3 * 1000 // 3 seconds default
        repeat: false
        onTriggered: {
            //            main.showFacebook();
            notifications.visible = false;
        }
    }

}
